/**
 * 1：进入游戏后选择任务后，点击加速按钮为艾迪加速，
 *    3秒内可能加速到速度：1、2、3、4、8、10、13，
 *    结果的计算方法为：60÷（2+X）的结果（x为前面的加速度数字），
 *    根据正确与否反馈
 * 2：进入游戏后选择任务后，点击加速按钮为艾迪加速，
 *    3秒内可能加速到速度：14、15、16、17，
 *    结果的计算公式是530-210÷（X-10）×10 的结果（x为前面的加速度数字），
 *    根据正确与否反馈
 */
// parent
var $app;
// 资源
var resources;
// 按钮
var $startBtn,$select_startBtn,$taskABtn,$speedBtn,$confirmBtn,$arrestBtn,$resultBtn,$replayBtn,$returnBtn;
// 输入框
// var $resultInput;
// layer
var $start_layer,$select_start_layer,$huo_layer,
	$base_layer,$back_layer,$thief_layer,$chara_layer,$tips_layer,$game_layer,$confirm_layer,$qian_layer;
// CanvasRenderingContext2D
var start_2d,select_start_2d,huo_2d,
	base_2d,back_2d,thief_2d,chara_2d,tips_2d,game_2d,confirm_2d,qian_2d;
var $keyboard,$keyboardKey;
var $success,$error;
var $progress;
window.onload=function(){
	$html           = G.dom('html');
	$app            = G.dom('#app');
	$startBtn       = G.dom('#startBtn');
	$taskABtn       = G.dom('#taskABtn');
	$speedBtn       = G.dom('#speedBtn');
	$select_startBtn= G.dom('#select_startBtn');
	$confirmBtn     = G.dom('#confirmBtn');
	$arrestBtn      = G.dom('#arrestBtn');
	$resultBtn      = G.dom('#resultBtn');
	$replayBtn      = G.dom('#replayBtn');
	$returnBtn      = G.dom('#returnBtn');
	$keyboard       = G.dom('#keyboard');
	$keyboardKey    = G.dom('#keyboardKey');
	$success        = G.dom('#success');
	$error          = G.dom('#error');
	$progress       = G.dom('#progress');

	// canvas
	$start_layer       = G.dom('#start_layer');
	$huo_layer         = G.dom('#huo_layer');
	$select_start_layer= G.dom('#select_start_layer');
	$base_layer        = G.dom('#base_layer');
	$back_layer        = G.dom('#back_layer');
	$thief_layer       = G.dom('#thief_layer');
	$chara_layer       = G.dom('#chara_layer');
	$tips_layer        = G.dom('#tips_layer');
	$game_layer        = G.dom('#game_layer');
	$confirm_layer     = G.dom('#confirm_layer');
	$qian_layer        = G.dom('#qian_layer');
	start_2d           = $start_layer.getContext('2d');
	huo_2d             = $huo_layer.getContext('2d');
	select_start_2d    = $select_start_layer.getContext('2d');
	base_2d            = $base_layer.getContext('2d');
	back_2d            = $back_layer.getContext('2d');
	thief_2d           = $thief_layer.getContext('2d');
	chara_2d           = $chara_layer.getContext('2d');
	tips_2d            = $tips_layer.getContext('2d');
	game_2d            = $game_layer.getContext('2d');
	confirm_2d         = $confirm_layer.getContext('2d');
	qian_2d            = $qian_layer.getContext('2d');

	// 屏幕适应
	G.resize($app,4,3);
	// 加载图片
	var load = new G.load();
	load.media([
		{name : 'background', path : './images/background.png', type : 'img'},
		{name : 'boy', path : './images/boy.png', type : 'img'},
		{name : 'title_anim', path : './images/title_anim.png', type : 'img'},
		{name : 'huo', path : './images/huo.png', type : 'img'},
		{name : 'huo_anim', path : './images/huo_anim.png', type : 'img'},
		{name : 'background2', path : './images/background2.png', type : 'img'},
		{name : 'taskA_title', path : './images/taskA_title.png', type : 'img'},
		{name : 'earth', path : './images/earth.png', type : 'img'},
		{name : 'earth_hover', path : './images/earth_hover.png', type : 'img'},
		{name : 'confirm', path : './images/confirm.png', type : 'img'},
		{name : 'tips_anim', path : './images/tips_anim.png', type : 'img'},
		{name : 'thief_anim', path : './images/thief_anim.png', type : 'img'},
		{name : 'game_speed', path : './images/game_speed.png', type : 'img'},
		{name : 'speed_small', path : './images/speed_small.png', type : 'img'},
		{name : 'speed_big', path : './images/speed_big.png', type : 'img'},
		{name : 'game_dataload', path : './images/game_dataload.png', type : 'img'},
		{name : 'sub', path : './images/sub.png', type : 'img'},
		{name : 'game_empty', path : './images/game_empty.png', type : 'img'},
		{name : 'chara_anim', path : './images/chara_anim.png', type : 'img'},
		{name : 'chara_error_anim', path : './images/chara_error_anim.png', type : 'img'},
		{name : 'chara_win_anim', path : './images/chara_win_anim.png', type : 'img'},
		{name : 'thief_error_anim', path : './images/thief_error_anim.png', type : 'img'},
		
		{name : 'start_music', path : './music/start_music.mp3', type : 'audio'},
		{name : 'game_start_music', path : './music/game_start_music.mp3', type : 'audio'},
		{name : 'error_music', path : './music/error_music.mp3', type : 'audio'},
		{name : 'game_zhuabu_music', path : './music/game_zhuabu_music.mp3', type : 'audio'},
		{name : 'win_music', path : './music/win_music.mp3', type : 'audio'},
		{name : 'click_music', path : './music/click_music.mp3', type : 'audio'}
		// {name : 'A', path : './audio/A.mp3', type : 'audio'}
	],function(ret){
		console.log('图片加载进度：',ret);
		$progress.style.width=(ret*100)+'%';
	},gameInit);
};
// 屏幕适应
window.onresize=function(){
	G.resize($app,4,3);
};
// 进入游戏初始化
function gameInit(ret){
	G.hide($progress);
	// 显示启动游戏层
	G.show($start_layer);
	// 缓存图片
	resources = ret;

	/**
	 * 进行启动游戏层的制作
	 */
	renderStartLayer.call(start_2d);
	/**
	 * 进行选择任务层的制作
	 */
	renderSelectStartLayer.call(select_start_2d);
	/**
	 * 进行游戏层的制作
	 */
	renderBaseLayer.call(base_2d);
	
	renderQianLayer.call(qian_2d);

	// 事件初始化
	$startBtn.addEventListener('click',startTo,false);
	$taskABtn.addEventListener('click',selectTaskA,false);
	$select_startBtn.addEventListener('click',selectStartTo,false);
	$confirmBtn.addEventListener('click',confirmTo,false);
	$speedBtn.addEventListener('click',addSpeedTo,false);
	$arrestBtn.addEventListener('click',arrestTo,false);
	$resultBtn.addEventListener('click',resultTo,false);
	$replayBtn.addEventListener('click',replayTo,false);
	$returnBtn.addEventListener('click',returnTo,false);
	$keyboardKey.addEventListener('click',keyboardKeyTo,false);
	// $qian_layer.addEventListener('mousemove',qianMove,false);
	
	resources['game_zhuabu_music'].loop='loop';
}
// 开始按钮点击
function startTo(){
	resources['click_music'].play();
	// console.log('开始');
	G.hide($start_layer);
	G.hide($huo_layer);
	G.hide($startBtn);
	// 显示选择任务层
	G.show($select_start_layer);
	// 显示任务A
	G.show($taskABtn);
	// 显示选择开始按钮
	G.show($select_startBtn);
}
// taskA被选中
var taskAAble=false;
function selectTaskA(){
	taskAAble=!taskAAble;
	if(taskAAble){
		resources['click_music'].play();
		$taskABtn.className='btn taskA active';
	}else{
		$taskABtn.className='btn taskA';
	}
}
// 选择开始按钮点击
function selectStartTo(){
	if(taskAAble){
		resources['click_music'].play();
		// console.log('选择开始');
		
		// 隐藏任务层
		G.hide($select_start_layer);
		G.hide($taskABtn);
		G.hide($select_startBtn);

		// 显示游戏层
		G.show($qian_layer);
		G.show($base_layer); // 1E2A3E
		G.show($back_layer);
		G.show($thief_layer);
		G.show($chara_layer);
		G.show($tips_layer);
		G.show($game_layer);
		G.show($confirm_layer);

		renderBackLayer.call(back_2d);
		renderTipsLayer.call(tips_2d);
		renderThiefLayer.call(thief_2d);
		setTimeout(function(){
			resources['game_start_music'].play();
		},300);
	}
}
// 加速点击
var speedLimit=[1,2,3,4,8,10,13];
function addSpeedTo(){
	resources['click_music'].play();
	if(!speedComplete){
		speeda+=0.5;
		var s=Math.floor(speeda);
		for(var i=0;i<7;i++){
			if(s==speedLimit[i]){
				speed=s;
			}
		}
		// 显示速度
		game_2d.clearRect(414,0,610,320);
		game_2d.drawImage(resources['game_speed'],414,0,610,320);
		// game_2d.drawImage(resources['sub'],650,190,67,42);
		game_2d.save();
		game_2d.fillStyle = '#ffffff';
		game_2d.font='bolder 35px Microsoft YaHei';
		game_2d.fillText(speed+'',665,225);
		// game_2d.font='bolder 35px Microsoft YaHei';
		// game_2d.fillText('米 / 秒',730,220);
		game_2d.restore();
	}else{
		console.log('数据分析中');
	}
}
// 确认层的点击
function confirmTo(){
	resources['click_music'].play();
	// console.log('确认');
	G.hide($confirm_layer);
	G.hide($confirmBtn);
	// 显示加速按钮
	G.show($speedBtn);
	// 监听加速参数
	addListener();
}
// function qianMove(e){
// 	// 坐标转换
// 	var event = G.event_offset(e,1024,768),offsetX=event.offsetX,offsetY=event.offsetY;
// 	// 确认按钮
// 	if(G.point_rect(offsetX,offsetY,590,225,67,42)){
// 		$html.style.cursor='pointer';
// 	}else{
// 		$html.style.cursor='';
// 	}
// 	console.log(1);
// }

// 抓捕按钮点击
var isWin=false;
function arrestTo(){
	resources['click_music'].play();
	resources['game_zhuabu_music'].play();
	G.hide($keyboard);
	G.hide($keyboardKey);
	G.hide($arrestBtn);
	G.hide($game_layer);
	renderBackLayer2.call(back_2d,function(){
		console.log('实施抓捕');
		if(parseInt(60/(2+speed))==resultNum){
			renderCharaLayer.call(chara_2d);
			isWin=true;
		}else{
			renderCharaLayer2.call(chara_2d);
			isWin=false;
		}
	});
}
// 结果的输入
function resultTo(){
	resources['click_music'].play();
	// console.log('结果');
	G.show($keyboard);
	G.show($keyboardKey);
}
// 键盘点击
var resultStr='0',resultNum=0;
function keyboardKeyTo(e){
	var el = e.target,flag=el.getAttribute('flag');
	if(!flag)return false;
	resources['click_music'].play();
	if(flag=='del'){//删除
		console.log('删除');
		if(resultStr.length==1){
			resultStr='0';
		}else if(resultStr.length>1){
			resultStr=resultStr.slice(0,-1);
		}
		resultNum=parseInt(resultStr,10);
		console.log(resultStr,resultNum);
	}else if(flag=='enter'){//确定
		console.log('确定');
		G.hide($keyboard);
		G.hide($keyboardKey);
	}else if(resultNum<100){
		resultStr+=flag;
		resultNum=parseInt(resultStr,10);
		console.log(resultStr,resultNum);
	}
	game_2d.clearRect(590,225,67,42);
	game_2d.save();
	game_2d.fillStyle = '#425879';
	game_2d.fillRect(590,225,67,42);
	game_2d.drawImage(resources['sub'],590,225,67,42);
	game_2d.fillStyle = '#ffffff';
	game_2d.font='bolder 30px Microsoft YaHei';
	game_2d.fillText(resultNum+'',595,258);
	game_2d.restore();
}
// 重玩点击
function replayTo(){
	resources['click_music'].play();
	resultStr='0';
	resultNum=0;
	G.hide($replayBtn);
	G.hide($returnBtn);
	G.hide($success);
	G.hide($error);
	console.log('重玩');
	back_2d.clearRect(0,0,1024,768);
	thief_2d.clearRect(0,0,1024,768);
	chara_2d.clearRect(0,0,1024,768);
	tips_2d.clearRect(0,0,1024,768);
	game_2d.clearRect(0,0,1024,768);
	confirm_2d.clearRect(0,0,1024,768);
	selectStartTo();
}
// 返回点击
function returnTo(){
	resources['click_music'].play();
	resultStr='0';
	resultNum=0;
	G.hide($replayBtn);
	G.hide($returnBtn);
	G.hide($success);
	G.hide($error);
	console.log('返回');
	// 显示任务层
	G.show($select_start_layer);
	G.show($taskABtn);
	G.show($select_startBtn);

	// 隐藏游戏层
	G.hide($qian_layer);
	G.hide($base_layer); // 1E2A3E
	G.hide($back_layer);
	G.hide($thief_layer);
	G.hide($chara_layer);
	G.hide($tips_layer);
	G.hide($game_layer);
	G.hide($confirm_layer);
	back_2d.clearRect(0,0,1024,768);
	thief_2d.clearRect(0,0,1024,768);
	chara_2d.clearRect(0,0,1024,768);
	tips_2d.clearRect(0,0,1024,768);
	game_2d.clearRect(0,0,1024,768);
	confirm_2d.clearRect(0,0,1024,768);
}
/**
 * 进行启动游戏层的制作【静态】
 */
function renderStartLayer(){
	resources['start_music'].play();
	var s = this;
	var r=1,ar=20,globalAlpha=0,aglobalAlpha=0.02,startAnim=setInterval(function(){
		s.clearRect(0,0,1024,768);
		s.save();
		s.fillRect(0,0,1024,768);
		s.globalAlpha=(globalAlpha+=aglobalAlpha);
		var grd=s.createRadialGradient(512,384,50,512,384,r+50);
		grd.addColorStop(0,'#ffffff');
		grd.addColorStop(1,'#000000');
		s.fillStyle=grd;
		s.beginPath();
		s.arc(512,384,r+=ar,0,2*Math.PI);
		s.fill();
		s.restore();
		if(r>=1000){
			clearInterval(startAnim);
			s.clearRect(0,0,1024,768);
			s.drawImage(resources['background'],0,0,1024,768);
			s.drawImage(resources['huo'],232,238,560,400);
			renderHuoLayer.call(huo_2d);
			// 显示火+使命召唤动画
			G.show($huo_layer);
		}
	},1000/60);
}
/**
 * 火动画层的绘制
 */
function renderHuoLayer(){
	var s = this;
	// 火动画 + 使命召唤字幕动画
	var huoAnimArr=[
		[0,0,138,115],
		[0,115,138,115],
		[0,234,138,115],
		[0,353,138,115]
	],fontAnimArr=[
		[0,0,693,278],[693,0,665,278],[1358,0,693,278],
		[0,278,693,278],[693,278,665,278],[1358,278,693,278],
		[0,556,693,278],[693,556,665,278],[1358,556,693,278],
		[0,834,693,278],[693,834,665,278],[1358,834,693,278],
		[0,1112,693,278],[693,1112,665,278],[1358,1112,693,278],
		[0,1390,693,278],[693,1390,665,278],[1358,1390,693,278]
	],huoIndex=0,fontIndex=0,Anim=setInterval(function(){
		s.clearRect(0,0,1024,768);
		var h=huoAnimArr[huoIndex],f=fontAnimArr[fontIndex];
		s.drawImage(resources['huo_anim'],h[0],h[1],h[2],h[3],372,425,280,200);
		s.drawImage(resources['title_anim'],f[0],f[1],f[2],f[3],(1024-f[2])/2,425,f[2],f[3]);
		s.drawImage(resources['boy'],412,238,200,530);
		if(++huoIndex>=4)huoIndex=0;
		if(++fontIndex>=18){
			clearInterval(Anim);
			// 开始按钮显示
			G.show($startBtn);
		}
	},50);
}
/**
 * 进行选择任务层的制作【静态】
 */
function renderSelectStartLayer(){
	var s = this;

	s.drawImage(resources['background2'],0,0,1024,768);

	s.save();
	s.fillStyle = '#ffffff';
	s.font='bolder 35px Microsoft YaHei';
	s.fillText('请选择一个绝密任务 ',350,174);
	s.restore();
}
/**
 * 进行游戏层的制作
 */
function renderBaseLayer(){
	var s = this;

	s.save();
	s.fillStyle='#1E2A3E';
	s.fillRect(0,0,1024,768);
	s.restore();
}
function renderBackLayer(){
	var s = this;
	// s.drawImage(resources['earth'],0,0,1024,468,0,300,1024,468);
	var flag=true,Anim=setInterval(function(){
		flag=!flag;
		s.clearRect(0,0,1024,850);
		s.drawImage(resources[flag?'earth':'earth_hover'],0,0,1024,850,0,468,1024,850);
		if(ThiefAnimComplete){
			clearInterval(Anim);
			renderGameLayer.call(game_2d);
		}
	},100);
}
function renderBackLayer2(callback){
	var s = this;
	var flag=true,n=0,Anim=setInterval(function(){
		flag=!flag;
		s.clearRect(0,0,1024,850);
		s.drawImage(resources[flag?'earth':'earth_hover'],0,0,1024,850,0,398,1024,850);
		if(++n>20){
			clearInterval(Anim);
			if(typeof callback==='function'){
				callback();
			}
		}
	},100);
}
// 小偷动画是否完成
var ThiefAnimComplete;
function renderThiefLayer(){
	ThiefAnimComplete=false;
	var s = this;
	s.save();
	// 小偷动画
	var thiefAnimArr=[
		[0,0,89,85],[89,0,89,85],
		[0,85,89,85],[89,85,89,85],
		[0,170,89,85]
	],deg=180,x=0,y=0,w=0,h=0,thiefIndex=0,Anim=setInterval(function(){
		s.clearRect(x-w/2,y-h/2,w,h);
		var t=thiefAnimArr[thiefIndex];
		var a=deg/180*Math.PI;
		x=712+550*Math.cos(a);y=850+550*Math.sin(a);
		w=t[2];h=t[3];
		// s.save();
		s.translate(x+t[2]/2,y+t[3]/2);
		s.rotate(Math.PI/180);
		s.translate(-(x+t[2]/2),-(y+t[3]/2));
		s.drawImage(resources['thief_anim'],t[0],t[1],t[2],t[3],x-t[2]/2,y-t[3]/2,t[2],t[3]);
		// s.restore();
		// r=400 cx=512 cy=768
		if(++thiefIndex>=5){
			thiefIndex=0;
			// clearInterval(Anim);
		}
		if(++deg>=270){
			clearInterval(Anim);
			s.restore();
			ThiefAnimComplete=true;
		}
	},25);
}
// 是否可以进入游戏结束统计
var isRenderThiefComplete=false,isRenderCharaComplete=false;
function renderThiefLayer2(time){
	isRenderThiefComplete=false;
	var s = this;
	s.save();
	// 小偷动画
	var thiefAnimArr=[
		[0,0,89,85],[89,0,89,85],
		[0,85,89,85],[89,85,89,85],
		[0,170,89,85]
	],deg=180,x=0,y=0,w=0,h=0,thiefIndex=0,Anim=setInterval(function(){
		s.clearRect(x-w/2,y-h/2,w,h);
		var t=thiefAnimArr[thiefIndex];
		var a=deg/180*Math.PI;
		x=812+700*Math.cos(a);y=850+700*Math.sin(a);
		w=t[2];h=t[3];
		if(++deg>=220){
			clearInterval(Anim);
			s.restore();
			s.clearRect(x-w/2,y-h/2,w,h);
			console.log('小偷完成');
			isRenderThiefComplete=true;
			if(isRenderCharaComplete&&isRenderThiefComplete){
				GameOver();
			}
			return;
		}
		s.translate(x+t[2]/2,y+t[3]/2);
		s.rotate(Math.PI/180);
		s.translate(-(x+t[2]/2),-(y+t[3]/2));
		s.drawImage(resources['thief_anim'],t[0],t[1],t[2],t[3],x-t[2]/2,y-t[3]/2,t[2],t[3]);
		// s.restore();
		// r=400 cx=512 cy=768
		if(++thiefIndex>=5){
			thiefIndex=0;
			// clearInterval(Anim);
		}
	},time);
}
function renderCharaLayer(){
	isRenderCharaComplete=false;
	var s = this;
	s.save();
	s.translate(-400+550*Math.cos(2*Math.PI)+41,1200+550*Math.sin(2*Math.PI)+64);
	s.rotate(Math.PI/2);
	s.translate(-(-400+550*Math.cos(2*Math.PI)+41),-(1200+550*Math.sin(2*Math.PI)+64));
	// 小黄人动画
	var charaAnimArr=[
		[0,0,82,128],[82,0,82,128],[164,0,79,128],[243,0,82,128],[325,0,78,128],[403,0,80,128],
		[0,124,81,128],[81,124,68,128],[149,124,68,128],[217,124,68,128],[285,124,63,128],[348,124,62,128],[410,124,68,128],
		[0,252,70,128],[70,252,71,128],[141,252,82,128]
	],deg=270,x=0,y=0,w=0,h=0,charaIndex=0,Anim=setInterval(function(){
		s.clearRect(x-w/2,y-h/2,w,h);
		var c=charaAnimArr[charaIndex];
		var a=deg/180*Math.PI;
		x=-400+550*Math.cos(a);y=1200+550*Math.sin(a);
		w=c[2];h=c[3];
		if(--deg<=235){
			clearInterval(Anim);
			s.restore();
			s.clearRect(x-w/2,y-h/2,w,h);
			console.log('黄人完成');
			isRenderCharaComplete=true;
			if(isRenderCharaComplete&&isRenderThiefComplete){
				GameOver();
			}
			return;
		}
		s.translate(x+c[2]/2,y+c[3]/2);
		s.rotate(-Math.PI/90);
		s.translate(-(x+c[2]/2),-(y+c[3]/2));
		s.drawImage(resources['chara_anim'],c[0],c[1],c[2],c[3],x-c[2]/2,y-c[3]/2,c[2],c[3]);
		if(++charaIndex>=16){
			charaIndex=0;
		}
	},1000/60);
	renderThiefLayer2.call(thief_2d,1000/60);
}
function renderCharaLayer2(){
	var s = this;
	s.save();
	s.translate(-400+550*Math.cos(2*Math.PI)+41,1200+550*Math.sin(2*Math.PI)+64);
	s.rotate(Math.PI/2);
	s.translate(-(-400+550*Math.cos(2*Math.PI)+41),-(1200+550*Math.sin(2*Math.PI)+64));
	// 小黄人动画
	var charaAnimArr=[
		[0,0,82,128],[82,0,82,128],[164,0,79,128],[243,0,82,128],[325,0,78,128],[403,0,80,128],
		[0,124,81,128],[81,124,68,128],[149,124,68,128],[217,124,68,128],[285,124,63,128],[348,124,62,128],[410,124,68,128],
		[0,252,70,128],[70,252,71,128],[141,252,82,128]
	],deg=270,x=0,y=0,w=0,h=0,charaIndex=0,Anim=setInterval(function(){
		s.clearRect(x-w/2,y-h/2,w,h);
		var c=charaAnimArr[charaIndex];
		var a=deg/180*Math.PI;
		x=-400+550*Math.cos(a);y=1200+550*Math.sin(a);
		w=c[2];h=c[3];
		if(--deg<=235){
			clearInterval(Anim);
			s.restore();
			s.clearRect(x-w/2,y-h/2,w,h);
			console.log('黄人完成');
			isRenderCharaComplete=true;
			if(isRenderCharaComplete&&isRenderThiefComplete){
				GameOver();
			}
			return;
		}
		s.translate(x+c[2]/2,y+c[3]/2);
		s.rotate(-Math.PI/90);
		s.translate(-(x+c[2]/2),-(y+c[3]/2));
		s.drawImage(resources['chara_anim'],c[0],c[1],c[2],c[3],x-c[2]/2,y-c[3]/2,c[2],c[3]);
		if(++charaIndex>=16){
			charaIndex=0;
		}
	},100);
	renderThiefLayer2.call(thief_2d,80);
}
function renderTipsLayer(){
	var s = this;
	// 提示牌动画
	var tipsAnimArr=[
		[0,0,820,657],[820,0,820,657],[1640,0,820,657],[2460,0,820,657],
		[0,657,820,663],[820,657,820,663],[1640,657,820,663],[2460,657,820,663],
		[0,1320,827,595],[827,1320,820,595],[1647,1320,820,595],[2467,1320,820,595],
		[0,1915,819,631],[819,1915,816,631],[1635,1915,818,631],[2453,1915,819,631],[3272,1915,820,631],
		[0,2546,820,639],[820,2546,820,639],[1640,2546,820,639],[2460,2546,820,639],
		[0,3185,820,637],[820,3185,820,637],[1640,3185,820,637]
	],tipsIndex=4,y=-450,Anim=setInterval(function(){
		s.clearRect(0,0,1024,768);
		var t=tipsAnimArr[tipsIndex];
		s.drawImage(resources['tips_anim'],t[0],t[1],t[2],t[3],(1024-t[2])/2,y,t[2],t[3]);
		if(y<-250){
			y+=10;
		}else{
			if(++tipsIndex>=24){
				clearInterval(Anim);
			}
		}
	},1000/60);
}
function renderGameLayer(){
	var s = this;
	back_2d.clearRect(0,0,1024,850);
	thief_2d.clearRect(0,0,1024,850);
	tips_2d.clearRect(0,0,1024,768);

	// 执行过渡效果
	var offUp=0,dir=true,Anim=setInterval(function(){
		s.clearRect(0,0,1024,850);
		if(dir){ // 向上移动
			s.drawImage(resources['earth'],0,0,1024,850,0,468+offUp,1024,850);
			s.drawImage(resources['tips_anim'],1640,3185,820,637,102,-250-offUp,820,637);
			if((offUp+=10)>=387){
				dir=false;
			}
		}else{ // 向下移动
			s.drawImage(resources['earth'],0,0,1024,850,0,468+offUp,1024,850);
			if((offUp-=10)<=-68){
				clearInterval(Anim);
				s.clearRect(0,0,1024,850);
				// 重绘
				back_2d.drawImage(resources['earth'],0,0,1024,850,0,468+offUp,1024,850);
				// s.drawImage(resources['game_speed'],414,0,610,320);
				// 加速提示牌动画
				var y=-320,Anim2=setInterval(function(){
					s.clearRect(414,y,610,320);
					s.drawImage(resources['game_speed'],414,y,610,320);
					if((y+=10)>0){
						clearInterval(Anim2);
						G.show($confirm_layer);
						renderConfirmLayer.call(confirm_2d);
						// 绘制速度牌上文字
						// s.drawImage(resources['sub'],650,190,67,42);
						// game_2d.save();
						// game_2d.fillStyle = '#ffffff';
						// game_2d.font='bolder 35px Microsoft YaHei';
						// game_2d.fillText('米 / 秒',730,220);
						// game_2d.restore();
					}
				},1000/60);
			}
		}
	},1000/60);
}
function renderConfirmLayer(){
	var s = this;
	G.show($confirmBtn);
	s.drawImage(resources['confirm'],0,0,1024,768);
}
function renderQianLayer(){
	var s = this;
	s.drawImage(resources['taskA_title'],20,20,330,90);
}
// 监听加速参数
var speed,speedComplete,speeda;
function addListener(){
	speed=1;
	speeda=1;
	speedComplete=false;
	// 1、2、3、4、8、10、13
	var listener=setTimeout(function(){
		clearTimeout(listener);
		G.hide($speedBtn);
		speedComplete=true;
		speeda=1;
		// 绘制数据在分析中...
		game_2d.clearRect(414,0,610,320);
		game_2d.drawImage(resources['game_dataload'],414,0,610,320);
		// 模拟数据分析动画
		setTimeout(function(){
			game_2d.clearRect(414,0,610,320);
			game_2d.drawImage(resources['game_empty'],414,0,610,320);
			game_2d.drawImage(resources['sub'],590,225,67,42);
			// 显示文字
			game_2d.save();
			game_2d.fillStyle = '#ffffff';
			game_2d.font='bolder 35px Microsoft YaHei';
			game_2d.fillText('大盗速度2米/秒，艾迪可加速',480,170);
			game_2d.fillText('到 '+speed+' 米/秒，一圈60米，遇见',480,213);
			game_2d.fillText('时间为',480,256);
			game_2d.restore();
			G.show($resultBtn);
		},1000);
		// 实施抓捕
		G.show($arrestBtn);
	},3000);
}

function GameOver(){
	resources['game_zhuabu_music'].pause();
	G.show($confirm_layer);
	confirm_2d.clearRect(0,0,1024,768);
	confirm_2d.save();
	confirm_2d.globalAlpha=0.5;
	confirm_2d.fillRect(0,0,1024,768);
	confirm_2d.restore();
	G.show($replayBtn);
	G.show($returnBtn);
	if(isWin){
		console.log('拦截成功');
		resources['win_music'].play();
		// 实现第一帧动画
		chara_2d.clearRect(0,0,1024,768);
		var t=[0,0,55,127];
		chara_2d.drawImage(resources['chara_win_anim'],t[0],t[1],t[2],t[3],(1024-t[2])/2+100,400,t[2],t[3]);
		thief_2d.clearRect(0,0,1024,768);
		t=[0,0,85,95];
		thief_2d.drawImage(resources['thief_error_anim'],t[0],t[1],t[2],t[3],(1024-t[2])/2,450,t[2],t[3]);
		toSuccess();
	}else{
		console.log('拦截失败');
		resources['error_music'].play();
		// 实现第一帧动画
		chara_2d.clearRect(0,0,1024,768);
		var t=[182,0,69,117];
		chara_2d.drawImage(resources['chara_error_anim'],t[0],t[1],t[2],t[3],(1024-t[2])/2,450,t[2],t[3]);
		toError();
	}
	isRenderCharaComplete=false;
	isRenderThiefComplete=false;
	isWin=false;
}
function toSuccess(){
	G.show($success);
	// 拦截成功动画
	var charaErrorAnimArr=[
		[0,0,55,127],[55,0,50,127],
		[0,127,49,123]
	],charaSuccessIndex=1,Anim=setInterval(function(){
		chara_2d.clearRect(0,0,1024,768);
		var t=charaErrorAnimArr[charaSuccessIndex];
		chara_2d.drawImage(resources['chara_win_anim'],t[0],t[1],t[2],t[3],(1024-t[2])/2+100,400,t[2],t[3]);
		if(++charaSuccessIndex>=3){
			clearInterval(Anim);
		}
	},100);
	// 小偷被拦截动画
	var thiefErrorAnimArr=[
		[0,0,85,95],[85,0,85,95],[170,0,85,95],
		[0,95,68,85]
	],thiefErrorIndex=1,Anim2=setInterval(function(){
		thief_2d.clearRect(0,0,1024,768);
		var t=thiefErrorAnimArr[thiefErrorIndex];
		thief_2d.drawImage(resources['thief_error_anim'],t[0],t[1],t[2],t[3],(1024-t[2])/2,450,t[2],t[3]);
		if(++thiefErrorIndex>=4){
			clearInterval(Anim2);
		}
	},100);
}
function toError(){
	G.show($error);
	// 摔倒动画
	var charaErrorAnimArr=[
		[182,0,69,117],[182,117,69,117],
		[76,0,106,74],[76,74,106,74],[76,148,106,74]
	],charaErrorIndex=1,Anim=setInterval(function(){
		chara_2d.clearRect(0,0,1024,768);
		var t=charaErrorAnimArr[charaErrorIndex];
		chara_2d.drawImage(resources['chara_error_anim'],t[0],t[1],t[2],t[3],(1024-t[2])/2,450,t[2],t[3]);
		if(++charaErrorIndex>=5){
			clearInterval(Anim);
		}
	},100);
}